﻿window.onload = function()
{
    //所有代码都在页面完全加载之后执行
    //先加载所有输入框对象
    var userName_text = document.getElementById("userName_text");
    var emailAddress_text = document.getElementById("emailAddress_text");
    var submit_step_1 = document.getElementById("submit_step_1");
    var answer1_text = document.getElementById("answer1_text");
    var answer2_text = document.getElementById("answer2_text");
    var submit_step_2 = document.getElementById("submit_step_2");

    var notification_step_1 = document.getElementById("notification_step_1");
    var notification_step_2 = document.getElementById("notification_step_2");
    
    //声明存储邮箱地址有效性的变量
    var emailCheckResult = false;

    submit_step_1.onclick = function()
    {
        if (userName_text.value.trim() == "")
        {
            notification_step_1.innerHTML = "请输入用户名";
            return false;
        }
        else if (emailAddress_text.value.trim() == "")
        {
            notification_step_1.innerHTML = "请输入邮箱地址";
            return false;
        }
        else if (emailCheckResult == false)
        {
            notification_step_1.innerHTML = "您输入的邮箱地址不合法";
            return false;
        }
        return true;
    };

    emailAddress_text.onkeyup = function()
    {
        var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (pattern.test(emailAddress_text.value.trim()))
        {
            emailCheckResult = true;
            emailAddress_text.style.backgroundColor = "#aeffae";
        }
        else
        {
            emailCheckResult = false;
            emailAddress_text.style.backgroundColor = "#ffb1b1";
        }
    };

    submit_step_2.onclick = function()
    {
        if (answer1_text.value.trim() == "")
        {
            notification_step_2.innerHTML = "请输入问题 1 的答案";
            return false;
        }
        else if (answer2_text.value.trim() == "")
        {
            notification_step_2.innerHTML = "请输入问题 2 的答案";
            return false;
        }
        return true;
    };
}